package com.haotunet.app.goldtreasure.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.GuideViewAdapter;

import java.util.ArrayList;
import java.util.List;

public class SplashActivity extends FragmentActivity {
	private int[] imageView = { R.drawable.guide_pic1, R.drawable.guide_pic2, R.drawable.guide_pic3,
			R.drawable.guide_pic4, };
	private ViewPager indicatorPager;
	private LinearLayout llPoint;
	private List<View> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉信息栏
		setContentView(R.layout.activity_splash);
		initView();
		addView();
		addPoint();
	}

	private void initView() {
		indicatorPager = (ViewPager) findViewById(R.id.guidence_pager);
		llPoint = (LinearLayout) findViewById(R.id.llPoint);
		indicatorPager.addOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				monitorPoint(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	private void addView() {
		list = new ArrayList<View>();
		// 将imageview添加到view
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		for (int i = 0; i < imageView.length; i++) {
			ImageView iv = new ImageView(this);
			iv.setLayoutParams(params);
			iv.setScaleType(ScaleType.FIT_XY);
			iv.setImageResource(imageView[i]);
			list.add(iv);
		}
		// 加入适配器
		indicatorPager.setAdapter(new GuideViewAdapter(list));
	}

	private void addPoint() {
		for (int i = 0; i < imageView.length; i++) {
			LinearLayout.LayoutParams pointParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			if (i < 1) {
				pointParams.setMargins(0, 0, 0, 0);
			} else {
				pointParams.setMargins(15, 0, 0, 0);
			}
			ImageView iv = new ImageView(this);
			iv.setLayoutParams(pointParams);
			iv.setBackgroundResource(R.drawable.point_normal);
			llPoint.addView(iv);
		}
		llPoint.getChildAt(0).setBackgroundResource(R.drawable.point_select);
	}

	private void monitorPoint(int position) {
		for (int i = 0; i < imageView.length; i++) {
			if (i == position) {
				llPoint.getChildAt(position).setBackgroundResource(R.drawable.point_select);
			} else {
				llPoint.getChildAt(i).setBackgroundResource(R.drawable.point_normal);
			}
		}
		// 3.当滑动到最后一个添加按钮点击进入，
		if (position == imageView.length - 1) {
			llPoint.setVisibility(View.GONE);
			list.get(position).setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(SplashActivity.this, MainActivity.class);
					intent.putExtra("type", "");
					startActivity(intent);
					overridePendingTransition(R.anim.fade, R.anim.hold);
					finish();
				}
			});
		}else{
			llPoint.setVisibility(View.VISIBLE);
		}
	}
}
